package com.wiley.loginPO;

import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import com.wiley.publicMethod.BasePage;
import com.wiley.publicMethod.LoggerControler;

import java.net.MalformedURLException;

public class LoginPage extends BasePage {

	/**
	 * 页面元素
	 */
	//我的
	public static final By mine_item_loc = By.id(BaseId + "mine_item");

	//点击登录按钮
	public static final By login_tv_loc = By.id(BaseId + "login_tv");

	//登录界面：用户
	public static final By login_user_loc = By.id(BaseId+"login_user");

	//登录界面：密码
	public static final By login_password_loc = By.id(BaseId + "login_password");

	//登录界面：登录好豆
	public static final By login_haodou_loc = By.id(BaseId+"login_haodou");
	
	//用户昵称
	public static final By nick_tv_loc = By.id(BaseId+"nick_tv");
	
	//设置
	public static final By setting_btn_loc = By.id(BaseId+"setting_btn");
	
	//退出登录
	public static final By tv_logout_loc = By.id(BaseId+"tv_logout");
	
	//确定
	public static final By right_bt_loc = By.id(BaseId+"right_bt");
	
	/**
	 * 操作方法
	 */
	//点击我的
	public void clickMineItem(){
		this.clickEle(mine_item_loc);
	}

	//登录按钮
	public void clickLoginButton(){
		this.clickEle(login_tv_loc);
	}

	//定位输入框：用户
	public void inputLoginUser(String username){
		this.sendKeys(login_user_loc,username);
	}
	
	//定位输入框：密码
	public void inputLoginPwd(String pwd){
		this.sendKeys(login_password_loc,pwd);
	}

	//点击登录好豆
	public void clickLoginHaoDou(){
		this.clickEle(login_haodou_loc);
	}

	//用户昵称
	public String getUserName(){
		WebElement ele = this.findEle(nick_tv_loc);
		return ele.getText();
	}
	
	//点击设置
	public void clickSetting(){
		this.clickEle(setting_btn_loc);
	}
	
	//点击退出登录
	public void clickLogout(){
		this.clickEle(tv_logout_loc);
	}
	
	//点击确定
	public void clickConfirm(){
		this.clickEle(right_bt_loc);
	}

	//正常登录
	public void loginOpt(){
		this.clickLoginButton();
		this.inputLoginUser("18250862110");
		this.inputLoginPwd("fgwvip123");
		this.clickLoginHaoDou();
	}
		
	/**
	 * 业务封装
	 * @throws MalformedURLException 
	 */
	//多账号登录验证
	public void loginWithAccounts(String username,Class name) throws MalformedURLException  {
		LoggerControler log = this.MyLogger(name);
		AndroidDriver driver = super.SetDriver();
		this.clickMineItem();
		if(super.isElementPresent(nick_tv_loc)){
			this.clickSetting();
			super.swipeToUp(500);
			this.clickLogout();
			this.clickConfirm();
		}
		this.clickLoginButton();
		this.inputLoginUser(username);
		log.info("本次输入的账号是："+username);
		this.inputLoginPwd("fgwvip123");
		this.clickLoginHaoDou();
		Assert.assertTrue(getUserName().equals("洛牧决"));
		super.captureScreen("Login_"+username);
		log.info("该case测试通过");
	}
}
